using System;
using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using I2.Loc;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace BCW.Friends
{
	public class FriendsUINotification : MonoBehaviour
	{
		public struct ActiveMessage
		{
			public string text;

			public string buttonText;

			public EventDelegate OnClick;
		}

		public static FriendsUINotification instance;

		private Color initialLableColor;

		[SerializeField]
		private GameObject _mainPanel;

		[SerializeField]
		public UILabel lbMessagePasiveNotification;

		[SerializeField]
		public UILabel lbMessageActiveNotification;

		public GameObject ActiveNotification;

		public GameObject PasiveNotification;

		public GameObject loadAnimation;

		public UIButton button;

		public UILabel buttonLable;

		private string msgFriendNotification = string.Empty;

		private string msgMessageNotification = string.Empty;

		private string msgInviteNotification = string.Empty;

		private List<string> msgError = new List<string>();

		private List<ActiveMessage> activeMsgs = new List<ActiveMessage>();

		private bool canShowNewFriend = true;

		private bool canShowNewMessage = true;

		private Coroutine controllerNotification;

		private bool canShowWarningMsg = true;

		private bool canShowNotification = true;

		private Tweener _tween;

		public static event Action showErrorOver;

		private void Awake()
		{
			instance = this;
			BCWDebug.LogError("ACTIVATE", "FRIDN");
			canShowNotification = true;
			canShowNewFriend = true;
			canShowNewMessage = true;
			initialLableColor = lbMessageActiveNotification.color;
		}

		private void Start()
		{
			if (controllerNotification != null)
			{
				CoroutineRunner.Instance.StopCoroutine(controllerNotification);
			}
			controllerNotification = CoroutineRunner.Instance.StartCoroutine(ControllerNotification());
		}

		private void OnEnable()
		{
			FriendsController.OnReciveNewFriends += AddInQueueNewFriend;
			FriendsController.OnReciveNewInvites += AddInQueueNewInvite;
			FriendsController.OnReciveNewMessages += AddInQueueNewMessage;
		}

		private void OnDisable()
		{
			FriendsController.OnReciveNewFriends -= AddInQueueNewFriend;
			FriendsController.OnReciveNewInvites -= AddInQueueNewInvite;
			FriendsController.OnReciveNewMessages -= AddInQueueNewMessage;
		}

		private void OnDestroy()
		{
			instance = null;
			FriendsController.OnReciveNewFriends -= AddInQueueNewFriend;
			FriendsController.OnReciveNewInvites -= AddInQueueNewInvite;
			FriendsController.OnReciveNewMessages -= AddInQueueNewMessage;
			if (controllerNotification != null)
			{
				CoroutineRunner.Instance.StopCoroutine(controllerNotification);
			}
		}

		public void AddInQueueNewMessage(Dictionary<string, uint> dictMessage)
		{
			if (FriendsUIController.currentTab != FriendsUIController.FriendUITab.None)
			{
				msgFriendNotification = string.Empty;
				return;
			}
			BCWDebug.LogError("New message", "FRIDN");
			if (dictMessage.Count == 1)
			{
				List<string> list = new List<string>(dictMessage.Keys);
				if (FriendsController.instance.playerDB.ContainsKey(list[0]))
				{
					msgMessageNotification = string.Format(ScriptLocalization.Get("Friends/src_Notification_Message1"), FriendsController.instance.playerDB[list[0]].name);
				}
			}
			else
			{
				msgMessageNotification = string.Format(ScriptLocalization.Get("Friends/src_Notification_Message2"), FriendsController.instance.CountNewMessages);
			}
		}

		public void AddInQueueNewFriend()
		{
			if (FriendsUIController.currentTab != FriendsUIController.FriendUITab.None)
			{
				msgFriendNotification = string.Empty;
				return;
			}
			BCWDebug.LogError("New friend", "FRIDN");
			if (FriendsController.instance.CountNewFriends != 0)
			{
				msgFriendNotification = ScriptLocalization.Get("Friends/src_Notification_Friend");
			}
			else
			{
				msgFriendNotification = string.Empty;
			}
		}

		public void AddInQueueNewInvite()
		{
			if (FriendsUIController.currentTab != FriendsUIController.FriendUITab.None)
			{
				msgFriendNotification = string.Empty;
				return;
			}
			BCWDebug.LogError("New invite", "FRIDN");
			msgInviteNotification = string.Format(ScriptLocalization.Get("Friends/src_Notification_Invite"), (uint)FriendsController.instance.InvitesIds.Count);
		}

		public void AddInQueueWarninMsg(ServerResponse msgType)
		{
			BCWDebug.LogError("MSG" + msgType, "FRIDN");
			switch (msgType)
			{
			case ServerResponse.Successful:
				msgError.Add(ScriptLocalization.Get("Friends/src_Notification_Succesful"));
				break;
			case ServerResponse.EntityExists:
				msgError.Add(ScriptLocalization.Get("Friends/src_Notification_InviteExists"));
				break;
			case ServerResponse.ServerFail:
				msgError.Add(ScriptLocalization.Get("Friends/src_Notification_ServerFail"));
				break;
			default:
				msgError.Add(ScriptLocalization.Get("Friends/src_Notification_InternalError"));
				break;
			}
		}

		public void AddInQueueJoinFriend(string playerId)
		{
			if (FriendsController.instance.IsFriend(playerId) && FriendsController.instance.playerDB.ContainsKey(playerId))
			{
				msgMessageNotification = string.Format(ScriptLocalization.Get("Friends/src_Notification_FriendJoin"), FriendsController.instance.playerDB[playerId].name);
			}
		}

		public void AddInQueueWarninMsg(string errorMsg)
		{
			msgError.Add(errorMsg);
		}

		public void AddInQueueActiveMsg(string msg, string buttonText, EventDelegate OnClick)
		{
			ActiveMessage item = default(ActiveMessage);
			item.text = msg;
			item.buttonText = buttonText;
			item.OnClick = OnClick;
			activeMsgs.Add(item);
		}

		public void ChangeMessage(string text)
		{
			lbMessageActiveNotification.color = Color.red;
			lbMessageActiveNotification.text = text;
			HOTween.To(_mainPanel.transform, 0.5f, new TweenParms().Delay(3f).Prop("localPosition", Vector3.down * 200f).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				canShowNotification = true;
				canShowWarningMsg = true;
			}));
		}

		public void ShowNotification(string message, string buttonText, EventDelegate OnClick)
		{
			BCWDebug.LogError(message, "FRIDN");
			canShowNotification = false;
			_tween = null;
			lbMessageActiveNotification.color = initialLableColor;
			lbMessageActiveNotification.text = message;
			ActiveNotification.SetActive(true);
			PasiveNotification.SetActive(false);
			loadAnimation.SetActive(false);
			button.gameObject.SetActive(true);
			buttonLable.text = buttonText;
			button.onClick.Clear();
			HOTween.To(_mainPanel.transform, 0.5f, new TweenParms().Prop("localPosition", Vector3.zero).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				if (FriendsUINotification.showErrorOver != null && !canShowWarningMsg)
				{
					FriendsUINotification.showErrorOver();
				}
				button.onClick.Add(OnClick);
				button.onClick.Add(new EventDelegate(delegate
				{
					if (_tween != null)
					{
						HOTween.Kill(_tween);
					}
					lbMessageActiveNotification.text = ScriptLocalization.Get("Friends/lb_TryToConnect");
					loadAnimation.SetActive(true);
					button.gameObject.SetActive(false);
				}));
				_tween = HOTween.To(_mainPanel.transform, 3f, new TweenParms().Prop("localPosition", Vector3.zero).OnComplete((TweenDelegate.TweenCallback)delegate
				{
					HOTween.To(_mainPanel.transform, 0.5f, new TweenParms().Prop("localPosition", Vector3.down * 200f).OnComplete((TweenDelegate.TweenCallback)delegate
					{
						canShowNotification = true;
						canShowWarningMsg = true;
					}));
				}));
			}));
		}

		public void ShowNotification(string message)
		{
			BCWDebug.LogError(message, "FRIDN");
			canShowNotification = false;
			lbMessagePasiveNotification.text = message;
			ActiveNotification.SetActive(false);
			PasiveNotification.SetActive(true);
			HOTween.To(_mainPanel.transform, 0.5f, new TweenParms().Prop("localPosition", Vector3.zero).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				if (FriendsUINotification.showErrorOver != null && !canShowWarningMsg)
				{
					FriendsUINotification.showErrorOver();
				}
				HOTween.To(_mainPanel.transform, 3f, new TweenParms().Prop("localPosition", Vector3.zero).OnComplete((TweenDelegate.TweenCallback)delegate
				{
					HOTween.To(_mainPanel.transform, 0.5f, new TweenParms().Prop("localPosition", Vector3.down * 200f).OnComplete((TweenDelegate.TweenCallback)delegate
					{
						canShowNotification = true;
						canShowWarningMsg = true;
					}));
				}));
			}));
		}

		private IEnumerator ControllerNotification()
		{
			string newMessage = string.Empty;
			while (true)
			{
				yield return new WaitForSeconds(1f);
				BCWDebug.LogError(string.Format("Active {0}  canShowNotif {1} ", base.gameObject.activeSelf.ToString(), canShowNotification), "FRIDN");
				if (!base.gameObject.activeSelf || !canShowNotification)
				{
					continue;
				}
				if (activeMsgs.Count != 0)
				{
					HOTween.To(_mainPanel.transform, 0.5f, new TweenParms().Prop("localPosition", Vector3.down * 200f));
					yield return new WaitForSeconds(0.5f);
					ShowNotification(activeMsgs[0].text, activeMsgs[0].buttonText, activeMsgs[0].OnClick);
					activeMsgs.RemoveAt(0);
					continue;
				}
				if (msgError.Count != 0 && canShowWarningMsg)
				{
					if (!canShowNotification)
					{
						HOTween.To(_mainPanel.transform, 0.5f, new TweenParms().Prop("localPosition", Vector3.down * 200f));
						yield return new WaitForSeconds(0.5f);
					}
					ShowNotification(msgError[0]);
					msgError.RemoveAt(0);
					canShowWarningMsg = false;
				}
				while (!canShowNotification || SceneManager.GetActiveScene().name.Equals("Tutorial"))
				{
					yield return new WaitForSeconds(0.5f);
				}
				BCWDebug.LogError("currentTab  " + FriendsUIController.currentTab, "FRIDN");
				if (FriendsUIController.currentTab == FriendsUIController.FriendUITab.None)
				{
					if (msgInviteNotification != string.Empty)
					{
						ShowNotification(msgInviteNotification);
						msgInviteNotification = string.Empty;
					}
					else if (msgFriendNotification != string.Empty && canShowNewFriend)
					{
						ShowNotification(msgFriendNotification);
						msgFriendNotification = string.Empty;
						canShowNewFriend = false;
						StartCoroutine(TimerForFriend());
					}
					else if (msgMessageNotification != string.Empty && canShowNewMessage)
					{
						ShowNotification(msgMessageNotification);
						msgMessageNotification = string.Empty;
						canShowNewMessage = false;
						StartCoroutine(TimerForMessage());
					}
				}
			}
		}

		private IEnumerator TimerForFriend()
		{
			yield return new WaitForSeconds(10f);
			canShowNewFriend = true;
		}

		private IEnumerator TimerForMessage()
		{
			yield return new WaitForSeconds(10f);
			canShowNewMessage = true;
		}
	}
}
